package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.po.AuthInfo;
import net.wanho.service.AuthInfoService;
import net.wanho.vo.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-10-22 14:13
 * Description：<描述>
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthInfoService authInfoService;

    @GetMapping("/auth-tree")
    public R authTree(){
        List<AuthInfo> list = authInfoService.getAuthTree();
        return R.success(list);
    }

    @GetMapping("/name-check")
    public R nameCheck(String authName){
        AuthInfo authInfo = authInfoService.findByAuthName(authName);
        return R.success(authInfo==null?true:false);
    }

    @GetMapping("/url-check")
    public R urlCheck(String authUrl){
        AuthInfo authInfo = authInfoService.findByAuthUrl(authUrl);
        return R.success(authInfo);
    }

    @GetMapping("/code-check")
    public R codeCheck(String authCode){
        AuthInfo authInfo = authInfoService.findByAuthCode(authCode);
        return R.success(authInfo);
    }

    @PostMapping("/auth-add")
    public R authAdd(@RequestBody AuthInfo authInfo){
        authInfoService.add(authInfo);
        return R.success("权限添加成功！");
    }

    @PutMapping("/auth-enable/{id}")
    public R authEnable(@PathVariable Integer id){
        authInfoService.authEnable(id);
        return R.success("权限启用成功！");
    }

    @PutMapping("/auth-disable/{id}")
    public R authDisable(@PathVariable Integer id){
        authInfoService.authDisable(id);
        return R.success("权限禁用成功！");
    }

    @PutMapping("/auth-update")
    public R authUpdate(@RequestBody AuthInfo authInfo){
        authInfoService.updateById(authInfo);
        return R.success(("权限修改成功！"));
    }

    @DeleteMapping("/auth-delete/{id}")
    public R authDelete(@PathVariable Integer id){
        authInfoService.authDelete(id);
        return R.success("权限删除成功！");
    }
}
